package com.lemon.smartsafety.ui.adapter.user;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.user.AutoLoginEvent;
import com.lemon.smartsafety.entity.event.user.DeleteUserEvent;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.safecloud.greendao.LoginUser;

import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class LoginUserItem implements AdapterItem<LoginUser> {
    private LoggerHelper loggerHelper = new LoggerHelper(LoginUserItem.class);


    private ImageView mImageArrow;
    private TextView  mItemText, txtDelete;
    private View itemMainLayout;

    private LoginUser mLoginUser;


    private int mPosition;


    @Override
    public int getLayoutResId() {
        return R.layout.item_login_user;
    }

    @Override
    public void bindViews(View root) {
        itemMainLayout = root.findViewById(R.id.itemMainLayout);
        mImageArrow = (ImageView) root.findViewById(R.id.imageArrow);
        mItemText = (TextView) root.findViewById(R.id.itemText);
        txtDelete = (TextView) root.findViewById(R.id.txtDelete);
    }


    @Override
    public void setViews() {
        itemMainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AutoLoginEvent event = new AutoLoginEvent();
                event.setTag(EventTag.TAG_AUTO_LOGIN_EVENT);
                event.setPosition(mPosition);
                event.setUser(mLoginUser);
                AppService.getBus().post(event);
            }
        });
        txtDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLoginUser == null) {
                    return;
                }
                DeleteUserEvent event = new DeleteUserEvent();
                event.setTag(EventTag.TAG_DELETE_LOGIN_USER_EVENT);
                event.setPosition(mPosition);
                AppService.getBus().post(event);
            }
        });
    }


    @Override
    public void handleData(LoginUser user, int position) {
        mPosition = position;
        mLoginUser = user;
        mItemText.setText(mLoginUser.getUserName());

    }


}
